<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				customerUserCache:'bro-js-tsampleBorrowApply-customerUser-cache'
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			formSubmit(){
				let _that = this
				if(_that.formdata.objStr){
					let list = JSON.parse(_that.formdata.objStr)
					for(let item of list){
						item.fileList = item.files
						for(let url of item.files){
							url.fileUrl = url.fileSaveName
						}
					}
					_that.formdata.objStr = JSON.stringify(list)
				}
				_that.om.formSave('tsampleBorrowApply/addTsampleBorrowApply',_that.formdata,_that)
			},
			initLoad(options){
				let _that = this
				let fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code)
				]
				if(fileId){
					ajaxArr.push(
						{
							url:'tsampleBorrowApply/getInfo',
							param:{
									id:fileId
								},
							method:'post'
						}
					)
				}
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posname"),mainData)
					changeEditor("customerInfo","customerUserId",'isSearchUser',true,mainData)
					changeEditor("customerInfo","customerUserId",{
						"link":'../../public/searchUser/searchUser',
						"linkParam": {
							cachename: _that.customerUserCache,
							isSingle:true
						}
					},null,mainData)
					if(_that.subEditorObj['productInfo']){
						let att = {}
						let configData = _that.subConfigData['productInfo']
						if(hasEditor("productInfo",'productName',configData)){
							att.titleTemplate = '<%= productName%>'
						}
						changeEditor("productInfo",'objStr',att,mainData)
					}
					
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push('id')
						for(let index in res[1].value.tsampleBorrowApply){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[1].value.tsampleBorrowApply[index])
								_that.$set(_that.formdata,index,res[1].value.tsampleBorrowApply[index])
							}
						}
						if(_that.subEditorObj['productInfo']){
							let dataList = res[1].value.tsampleBorrowApply_data
							_that.subEditorObj['productInfo'].workList = dataList
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'objStr',JSON.stringify(dataList))
							_that.$set(_that.formdata,'objStr',JSON.stringify(dataList))
						}
					}
				_that.$nextTick(function(){
					if(!_that.$refs.autoform.formdata.zposId){
						_that.$refs.autoform.initSelectVal("topinfo","zposId")
					}else{
						_that.$refs.autoform.initSelectIndex("topinfo","zposId")
					}
				})
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		}, 
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				for (var fieldId in _that.subEditorObj) {
					var editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key:_that.customerUserCache,
					success:(res)=>{
						if(res.data){
							let d = res.data[0]
							let formdata = _that.$refs.autoform.formdata
							formdata.customerUserId = d.id
							formdata.customerUserName = d.username
							uni.removeStorage({
								key:_that.customerUserCache,
							})
						}
					}
				})
			}
		}
	}
</script>
<style></style>                                                                                                                                                                                                                                                                                                                  
